﻿using PermissionManager.Model;
using PermissionsManager.Models;
using PermissionsManager.BLL;
using System.Collections.Generic;
using System.Web.Mvc;
using PermissionManager.BLL;
using PermissionManager.Attributes;

namespace PermissionsManager.Controllers
{
    public class ModuleController : Controller
    {

        private ModuleService ModuleService = new ModuleService();
        
        /// <summary>
        /// 组管理页面
        /// </summary>
        /// <returns></returns>
        
        public ActionResult ModuleManage()
        {
            return View();
        }
        /// <summary>
        /// 新增组
        /// </summary>
        /// <returns></returns>        
        public ActionResult ModuleAdd()
        {
            return View();
        }
        /// <summary>
        /// 得到所有组信息
        /// </summary>
        /// <returns></returns>
        [Describe("查看组")]
        public JsonResult GetModule()
        {
            int count = 0;
            List<Module> modules = ModuleService.QueryList(a=>true);
            GetJsonToTable<Module> getJson = new GetJsonToTable<Module>()
            {
                msg = "",
                code = 0,
                count = ModuleService.GetCount(a=>a.Id!=0,out count),
                data = modules
            };
            return Json(getJson,JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 新增组操作
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        [Describe("新增组")]
        public JsonResult AddModule(Module module)
        {
            Opretation opretation = new Opretation()
            {
                Success= ModuleService.Add(module)
            };
            return Json(opretation);
        }
        /// <summary>
        /// 删除组
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [Describe("删除组")]
        public JsonResult DelModule(int Id)
        {
            Opretation opretation = new Opretation()
            {
                Success = ModuleService.Delete(Id)
            };
            return Json(opretation);
        }
        /// <summary>
        /// 修改组
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        [Describe("修改组")]
        public JsonResult UpdateModule(Module module)
        {           
            Opretation opretation = new Opretation()
            {
                Success = ModuleService.Update(module)
            };
            return Json(opretation);
        }
        /// <summary>
        /// 修改组状态
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        [Describe("修改组状态")]
        public JsonResult UpdateModuleStatu(Module module)
        {
            Module module1 = ModuleService.Query(a => a.Id == module.Id);
            module1.IsEnable = module.IsEnable;
            Opretation opretation = new Opretation()
            {
                Success = ModuleService.Update(module1)
            };
            return Json(opretation);
        }

    }
}